package codeRandomThoughts.Test343整数拆分;

public class Solution {
    public static int integerBreak(int n) {
        //2 <= n <= 58
        int[] dp = new int[n + 2];
        dp[2] = 1;
        dp[3] = 2;

        for (int i = 4; i <= n; i++) {
            for (int j = 2; j <= i - j; j++) {
                //每个dp[i]都能由之前的两个dp[i]相乘得到
                int part1 = Integer.max(dp[j], j);//如果拆分乘积还没有不拆分大,就直接用不拆分的值
                int part2 = Integer.max(dp[i - j], i - j);
                dp[i] = Integer.max(dp[i], part1 * part2);
            }
        }

        return dp[n];
    }

    public static void main(String[] args) {
        System.out.println("integerBreak(10) = " + integerBreak(10));
    }
}
